package com.nhn.ssi.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.ibatis.sqlmap.client.SqlMapClient;

public class SqlMapGlobals implements ApplicationContextAware{
	private static final Log log = LogFactory.getLog(SqlMapGlobals.class);
	
	private static Map<String, SqlMapClient> maps = new HashMap<String, SqlMapClient>();
	
	private ApplicationContext applicationContext;

	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;

	}	
	
	public static SqlMapClient getSqlMap(String key){
		return maps.get(key);
	}
	
	public void initMap(){
		String[] sqlmapList = this.applicationContext.getBeanNamesForType(SqlMapClient.class);
		for(int i=0; i<sqlmapList.length; i++){
			maps.put(sqlmapList[i], (SqlMapClient)applicationContext.getBean(sqlmapList[i]));
		}
		log.debug("SqlMap initialized:" + maps);
	}

}
